<template>
  <div class="box">
    <div class="title">相关推荐</div>
    <div class="content">
      <div class="item" v-for="item in this.resMv.resMv.mvs" :key="item.id">
        <div class="left">
          <div class="count">
            <i class="el-icon-video-play"></i>
            {{
              item.playCount >= 10000
                ? (item.playCount / 10000).toFixed(0) + "万"
                : item.playCount
            }}
          </div>
          <el-image :src="item.cover" fit="contain" @click="play(item)" lazy>
            <div slot="placeholder" class="image-slot">
              <el-image
                src="https://s4.music.126.net/style/web2/img/default/default_album.jpg"
                @click="play(item.id)"
                lazy
              ></el-image>
            </div>
          </el-image>
        </div>
        <div class="right">
          <div>{{ item.name }}</div>
          <div>by{{ item.artistName }}</div>
        </div>
      </div>
    </div>
    <div class="loading" v-if="loading === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  computed: {
    ...mapState(['resMv']),
  },
  mounted() {
    this.resMv.resMv.mvs;
  },
  data() {
    return {
      loading: false,
    };
  },
  methods: {
    ...mapActions(['saveMVurl', 'saveResMv', 'saveMvBes', 'saveArtistsItem']),
    // 播放MV
    async play(e) {
      // 获取到MV的地址
      this.$api.mvUrl({ id: parseInt(e.id), cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveMVurl({ mvUrl: ret.data.url });
      });
      this.getArtists(e.artistId);
      this.getResMv(e.id);
      this.getItem(e.id);
    },

    // 获取相似MV
    async getResMv(e) {
      this.$api.simiMv({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveResMv({ resMv: ret });
      });
    },

    // 获取MV基础信息
    async getItem(e) {
      this.loading = true;
      this.$api.mvDetail({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveMvBes({ mvBes: ret });
        this.$router.push(`/mvPlay?id=${e}`);
        this.loading = false;
      });
    },

    // 获取MV作者
    async getArtists(e) {
      window.sessionStorage.setItem('artists', e);
      this.$api.arDetail({ id: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveArtistsItem({ artistsItem: ret.data });
      });
    },
  },
};
</script>

<style scoped>
.box {
  margin-left: 15px;
  display: flex;
  flex-direction: column;
}

.title {
  font-size: 20px;
  margin-bottom: 32px;
}

.item {
  display: flex;
  margin-bottom: 15px;
}

.content {
  width: 100%;
}

.left {
  display: flex;
  flex-direction: column;
  position: relative;
}

.el-image {
  width: 200px;
  height: 100px;
}

.right {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.count {
  position: absolute;
  top: 0;
  right: 10px;
  z-index: 10;
  color: #fff;
  font-weight: 700;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}
</style>
